/**
 * 
 */
package org.vsg.stock.basic.persistence.file;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vsg.stock.basic.domain.StockTick;

/**
 * @author vison
 *
 */
public class FolderReadTickFileProcessor implements ReadTickFileProcessor {

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	private SimpleDateFormat dftime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	

	/**
	 * set the logger for develope code , do not use System.print(ln) to out print message.
	 */
	private static final Logger logger = LoggerFactory.getLogger(FolderReadTickFileProcessor.class);
	
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.persistence.file.ReadTickFileProcessor#readData(java.io.File, java.lang.String, java.util.Date)
	 */
	@Override
	public List<StockTick> readData(File parentFolder, String stockCode,
			Date date) throws FileNotFoundException {
		logger.debug("reade tick data ..... ");
		// TODO Auto-generated method stub
		File dateFolder = new File(parentFolder, sdf.format(date));
		File stockFile = new File(dateFolder , stockCode.toUpperCase()+".txt");

		List<StockTick> allStockTicks = new LinkedList<StockTick>();		
		
		InputStream is = new BufferedInputStream(new FileInputStream(stockFile) );
		
		try {
			List<String> dataLines = IOUtils.readLines(is);
			String line = null;
			StockTick stkTick = null;
			for (Iterator<String> linesIter = dataLines.iterator() ; linesIter.hasNext();) {
				line = linesIter.next();
				
				try {
					if (!line.trim().equals("")) {
						String[] sprLine = line.split(",");
						stkTick = new StockTick();
						stkTick.setCode( sprLine[0] );
						stkTick.setDateTime( dftime.parse( sprLine[1] ) );
						stkTick.setLastCurrentPrice( Double.parseDouble( sprLine[2] ) );
						stkTick.setSumTick( (int)Double.parseDouble(sprLine[3] ) );
						stkTick.setAmountVol( Double.parseDouble( sprLine[4] ) );
						stkTick.setPresentTickVol( Integer.parseInt( sprLine[5] ) );
						
						stkTick.setTickType( sprLine[6]);
						
						stkTick.setConsignedBuy1Price( Double.parseDouble( sprLine[7] ) );
						stkTick.setConsignedBuy1Vol( Long.parseLong( sprLine[8] ) );
						stkTick.setConsignedBuy2Price( Double.parseDouble( sprLine[9] ) );
						stkTick.setConsignedBuy2Vol( Long.parseLong( sprLine[10] ) );
						stkTick.setConsignedBuy3Price( Double.parseDouble( sprLine[11] ) );
						stkTick.setConsignedBuy3Vol( Long.parseLong( sprLine[12] ) );
						stkTick.setConsignedBuy4Price( Double.parseDouble( sprLine[13] ) );
						stkTick.setConsignedBuy4Vol( Long.parseLong( sprLine[14] ) );
						stkTick.setConsignedBuy5Price( Double.parseDouble( sprLine[15] ) );
						stkTick.setConsignedBuy5Vol( Long.parseLong( sprLine[16] ) );									

						stkTick.setConsignedSell1Price( Double.parseDouble(  sprLine[17] ) );
						stkTick.setConsignedSell1Vol( Long.parseLong( sprLine[18] ));
						stkTick.setConsignedSell2Price( Double.parseDouble(  sprLine[19] ) );
						stkTick.setConsignedSell2Vol( Long.parseLong( sprLine[20] ));
						stkTick.setConsignedSell3Price( Double.parseDouble(  sprLine[21] ) );
						stkTick.setConsignedSell3Vol( Long.parseLong( sprLine[22] ));
						stkTick.setConsignedSell4Price( Double.parseDouble(  sprLine[23] ) );
						stkTick.setConsignedSell4Vol( Long.parseLong( sprLine[24] ));
						stkTick.setConsignedSell5Price( Double.parseDouble(  sprLine[25] ) );
						stkTick.setConsignedSell5Vol( Long.parseLong( sprLine[26] ));
						stkTick.setTotalTick( Integer.parseInt( sprLine[27] ) );
						allStockTicks.add( stkTick );
						
					}
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					is = null;
				}
			}
		}
		return allStockTicks;
	}


	@Override
	public boolean checkFileExist(File parentFolder, String stockCode, Date date){
		// TODO Auto-generated method stub
		File dateFolder = new File(parentFolder, sdf.format(date));
		File stockFile = new File(dateFolder , stockCode.toUpperCase()+".txt");
		
		return stockFile.exists();
	}

	
	
}
